package com.yhyvr.api.service.store;

import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderRefundMapper;
import com.lz.mybatis.jdbc.auto.model.*;
import com.lz.tool.ListUtils;
import com.yhyvr.pojo.exception.ApiException;
import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.order.ApiOrderInfoReq;
import com.yhyvr.pojo.req.api.store.order.ApiOrderRefundGoodsUpdateReq;
import com.yhyvr.pojo.resp.api.ApiRespBody;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.store.order.ApiOrderRefundGoodsResp;
import com.yhyvr.service.ProxyOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by tech6 on 2016/10/27.
 * 订单取消退货
 */

@Service
public class ApiOrderRefundService {

    @Resource
    private ProxyOrderService orderService;

    @Resource
    private BoProxyOrderMapper boProxyOrderMapper;

    @Resource
    private BoProxyOrderRefundMapper boProxyOrderRefundMapper;
    /**
     *退货信息完善
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> refundGoodsUpdate(ApiReqData<ApiOrderRefundGoodsUpdateReq> apiReqData ){
            ApiOrderRefundGoodsUpdateReq apiUnifiedorderReq = apiReqData.getBody();
                String orderId = apiUnifiedorderReq.getOrderId();
                BoProxyOrderRefund boProxyOrder =  boProxyOrderRefundMapper.selectByPrimaryKey(orderId);
                if(boProxyOrder.getOrderAuditState()==2) {
                        orderService.refundOrderUpdate(apiUnifiedorderReq, apiReqData.getCookieUserId());
                }else {
                    throw new ApiException("未审核通过不能完善信息！",0,"7777",apiReqData.getHead().getCrossDomain());
                }
                return ApiRespData.buildSucc();

    }




    /**
     * 取消订单详情
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiOrderRefundGoodsResp> orderRefundInfo(ApiReqData<ApiOrderInfoReq> apiReqData){
            ApiOrderInfoReq apiUnifiedorderReq = apiReqData.getBody();
                BoProxyOrderRefundExample boProxyOrderExample = new BoProxyOrderRefundExample();
                boProxyOrderExample.createCriteria().andOrderIdEqualTo(apiUnifiedorderReq.getOrderId()).andProxyIdEqualTo(apiReqData.getCookieUserId());
                BoProxyOrderRefund boProxyOrder = ListUtils.getFirst(boProxyOrderRefundMapper.selectByExample(boProxyOrderExample));
                if(boProxyOrder==null){
                    throw new ApiException("该订单没有退货信息！",0,"40002",apiReqData.getHead().getCrossDomain());
                }else {
                    ApiOrderRefundGoodsResp apiOrderRefundGoodsResp = new ApiOrderRefundGoodsResp();
                    apiOrderRefundGoodsResp.setConsigneePhone(boProxyOrder.getConsigneePhone());
                    apiOrderRefundGoodsResp.setConsignee(boProxyOrder.getConsigneePhone());
                    apiOrderRefundGoodsResp.setOrderState(boProxyOrder.getOrderAuditState() + "");
                    apiOrderRefundGoodsResp.setExpressCompany(boProxyOrder.getOrderExpressCompany());
                    apiOrderRefundGoodsResp.setExpressNumber(boProxyOrder.getOrderExpressNumber());
                    apiOrderRefundGoodsResp.setOrderId(boProxyOrder.getOrderId());
                    apiOrderRefundGoodsResp.setShippingAddress(boProxyOrder.getShippingAddress());
                    apiOrderRefundGoodsResp.setOrderMessage(boProxyOrder.getOrderAuditMessage());
                    apiOrderRefundGoodsResp.setRefundCase(boProxyOrder.getRefundCase());
                    apiOrderRefundGoodsResp.setRefundNotes(boProxyOrder.getRefundNotes());
                    //封装订单详情
                    return ApiRespData.buildSucc(apiOrderRefundGoodsResp);
                }
    }

}
